<template>
  <div>
    <div class="card" style="margin-bottom: 5px;">
      <el-input v-model="name" style="width: 300px; margin-right: 10px" placeholder="请输入名称查询"></el-input>
      <el-button type="primary" @click="getDataList">查询</el-button>
      <el-button type="info" style="margin: 0 10px" @click="reset">重置</el-button>
    </div>

    <div class="card" style="margin-bottom: 5px">
      <div style="margin-bottom: 10px">
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </div>
      <el-table :data="tableData" stripe>

        <el-table-column label="ID" prop="id"></el-table-column>
        <el-table-column label="头像">
          <template slot-scope="scope">
            <el-image :src="`${baseUrl}/image/download/${scope.row.avatar}`"
              style="width: 40px; height: 40px; border-radius: 50%"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="用户名" prop="username"></el-table-column>
        <el-table-column label="姓名" prop="name"></el-table-column>
        <el-table-column label="性别" prop="gender"></el-table-column>
        <el-table-column label="年龄" prop="age"></el-table-column>
        <el-table-column label="手机号" prop="phone"></el-table-column>
        <el-table-column label="邮箱" prop="email"></el-table-column>
        <!-- <el-table-column label="角色" prop="role">
          <template slot-scope="scope">
            <span v-if="scope.row.role === 'ADMIN'">管理员</span>
          </template>
        </el-table-column> -->
        <el-table-column label="操作" align="center" width="160">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="card">
      <el-pagination background layout="prev, pager, next" :page-size="pageSize" :current-page="pageNum" :total="total"
        @current-change="handlePageChange">
      </el-pagination>
    </div>

    <el-dialog title="信息" width="40%" :visible.sync="formVisible" :close-on-click-modal="false">
      <el-form :model="form" label-width="100px" style="padding-right: 50px">
        <el-form-item label="头像">
          <el-upload class="avatar-uploader" :action="`${baseUrl}/image/upload`" :show-file-list="false"
            :on-success="handleAvatarSuccess">
            <img v-if="form.avatar" :src="`${baseUrl}/image/download/${form.avatar}`" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="账号">
          <el-input :disabled="edit" v-model="form.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="form.gender">
            <el-radio label="男" value="男">男</el-radio>
            <el-radio label="女" value="女">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input-number v-model="form.age" :min="1" :max="120" controls-position="right" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" maxlength="11" />
        </el-form-item>

        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱地址" />
        </el-form-item>
      </el-form>

      <template slot="footer">
        <span class="dialog-footer">
          <el-button @click="formVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">保 存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, createUser, deleteUser, updateUser } from "@/api/user";

export default {
  data() {
    return {
      edit: true,
      pageNum: 1,
      pageSize: 5,
      total: 0,
      formVisible: false,
      form: {},
      tableData: [],
      name: null,
      baseUrl: process.env.VUE_APP_BASE_URL,
    }
  },
  methods: {
    async getDataList() {
      const params = {
        current: this.pageNum,
        size: this.pageSize,
        name: this.name

      }
      const res = await listUser(params)
      if (res.code === 200) {
        this.tableData = res.data.records
        this.total = res.data.total
      }

    },
    handleAdd() {
      this.form = {
        avatar: '',
        username: '',
        name: '',
        gender: '男',
        age: 18,
        phone: '',
        email: ''
      }
      this.edit = false
      this.formVisible = true
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.formVisible = true
      this.edit = true
    },
    async add() {
      const res = await createUser(this.form)
      if (res.code === 200) {
        this.getDataList()
        this.$message.success('新增成功')
        this.formVisible = false
      } else {
        this.$message.error(res.msg)
      }
    },
    async update() {
      const res = await updateUser(this.form)
      if (res.code === 200) {
        this.getDataList()
        this.$message.success('修改成功')
        this.formVisible = false
      } else {
        this.$message.error(res.msg)
      }
    },
    save() {
      this.form.id ? this.update() : this.add()
    },
    handleDelete(id) {
      this.$confirm('删除后数据无法恢复，您确定删除吗?', '删除确认', {
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        deleteUser(id).then(res => {
          if (res.code === 200) {
            this.getDataList()
            this.$message.success('操作成功')
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch(() => {
        // 用户点击取消
        this.$message.info('已取消删除')
      })
    },
    reset() {
      this.name = null
      this.getDataList()
    },
    handleAvatarSuccess(res, file) {
      console.log(res);

      this.form.avatar = res;
    },
    handlePageChange(val) {
      this.pageNum = val
      this.getDataList()
    }
  },
  created() {
    this.getDataList()
  }
}
</script>


<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>